import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import MyNav from '../views/MyNav.vue'
import ProductIssueShow from '../views/ProductIssueShow.vue'
import ProductDetails from '../views/ProductDetails.vue'
import Personal from '../views/Personal.vue'
import Order from '../views/Order.vue'
import EssayIssue from '../views/EssayIssue.vue'
import Cart from '../views/Cart.vue'
import Community from '../views/Community.vue'
import CommunityMenu from '../views/CommunityMenu.vue'
import ProductManIssue from '../views/ProductManIssue.vue'
import ProductWomanIssue from '../views/ProductWomanIssue.vue'
import PaySuccess from '../views/PaySuccess.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    components: {
      default: Home,
      'header': MyNav
    }
  },
  //商品分类页
  {
    path: '/productmanissue',
    name: 'ProductManIssue',
    components: {
      default: ProductManIssue,
      'header': MyNav
    }
  },
  {
    path: '/productwomanissue',
    name: 'ProductWomanIssue',
    components: {
      default: ProductWomanIssue,
      'header': MyNav
    }
  },
  {//支付成功页面
    path: '/paysuccess',
    name: 'PaySuccess',
    components: {
      default: PaySuccess,
      'header': MyNav
    },
  },
  {//商品分类展示页
    path: '/productissueshow',
    name: 'ProductIssueShow',
    components: {
      default: ProductIssueShow,
      'header': MyNav
    }
  },
  {//商品详情
    path: '/productdetails',
    name: 'ProductDetails',
    components: {
      default: ProductDetails,
      'header': MyNav
    }
  },
  {//个人中心页
    path: '/personal',
    name: 'Personal',
    components: {
      default: Personal,
      'header': MyNav
    },
    beforeEnter(to, from, next) {
      let token = localStorage.getItem('token')
      if (token) {
        next()
      } else {
        alert('请先登录')
        next('/')
      }
    }
  },

  {//订单页
    path: '/order',
    name: 'Order',
    components: {
      default: Order,
      'header': MyNav
    }
  },
  {//文章分类页
    path: '/essayissue',
    name: 'EssayIssue',
    components: {
      default: EssayIssue,
      'header': MyNav
    }
  },
  //购物车
  {
    path: '/cart',
    name: 'Cart',
    components: {
      default: Cart,
      'header': MyNav
    },
    beforeEnter(to, from, next) {
      let token = localStorage.getItem('token')
      if (token) {
        next()
      } else {
        alert('请先登录')
        next('/')
      }
    }
  },
  {//文章创作页
    path: '/community',
    name: 'Community',
    components: {
      default: Community,
      'header': MyNav
    }
  },
  {//文章创作页
    path: '/communitymenu',
    name: 'CommunityMenu',
    components: {
      default: CommunityMenu,
      'header': MyNav
    }
  }
]
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
